<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@include file="../head.jsp"%>
<html>
<head>
<script type="text/javascript">
(function(){
    Ext.onReady(function(){
        Ext.QuickTips.init();
        var key = '';
        var id = '';

        Ext.define('SysOperator', {
            extend: 'Ext.data.Model',
            fields: [
                {name:'id',type:'string'},
                {name:'name',type:'string'},
                {name:'sex',type:'string'},
                {name:'phone',type:'string'},
                {name:'qq',type:'string'},
                {name:'mail',type:'string'},
                {name:'departmentId',type:'string'},
                {name:'departmentName',type:'string'},
                {name:'idCard',type:'string'},
                {name:'version',type:'int'},
                {name:'status',type:'int'}
            ]
        });

        var queryName = Ext.create('Ext.form.field.Text',{
            labelWidth:55,
            id:'queryName',
            name:'queryName'
        });

        var departmentStore = Ext.create('Ext.data.Store', {
            model: 'commonModel',
            proxy: {
                type:'ajax',
                url:'/fsxt/sysBase!getDepartment.action'
            },
            autoLoad: true
        });

        var departmentQueryStore = Ext.create('Ext.data.Store', {
            model: 'commonModel',
            proxy: {
                type:'ajax',
                url:'/fsxt/sysBase!getDepartment.action'
            },
            autoLoad: true,
            listeners:{
                load:function(){
                    var insert = Ext.create('commonModel',{value:'',label:'0-全部'});
                    this.insert(0,insert);
                }
            }
        });

        var departmentCfg = {
            queryMode: 'local',
            displayField: 'label',
            valueField: 'value'
        };
        Ext.apply(departmentCfg,{store:departmentQueryStore});
        var queryDepartment = Ext.create('Ext.form.ComboBox',departmentCfg);

        var store = Ext.create('Ext.data.Store', {
            model: 'SysOperator',
            pageSize:20,
            proxy: {
                type:'ajax',
                url:'/fsxt/sys/sysOperator!getSysOperator.action',
                reader: {
                    root: 'root',
                    totalProperty: 'totalProperty'
                }
            },
            autoLoad: true,
            listeners:{
                beforeload:function(){
                    store.proxy.extraParams = {
                        name:encodeURIComponent(queryName.getValue()),
                        departmentId:queryDepartment.getValue()
                    }
                }
            }
        });

        var grid = Ext.create('Ext.grid.Panel', {
            title: '人员管理',
            store: store,
            height: '100%',
            width: '100%',
            loadMask: true,
            autoScroll:true,
            columns:[
                Ext.create('Ext.grid.RowNumberer'),
                {text: "人员名称",dataIndex:'name',sortable: true},
                {text: "性别",dataIndex:'sex',sortable: true},
                {text: "电话",dataIndex:'phone',sortable: true},
                {text: "qq号码",dataIndex:'qq',sortable: true},
                {text: "邮箱",dataIndex:'mail',sortable: true},
                {text: "所属部门",dataIndex:'departmentName',sortable: true},
                {text: "状态",dataIndex:'status',sortable: true,
                    renderer:function(value){
                        if(value == 1)
                            return '<font color="blue">启用</font>';
                        return '<font color="red">冻结</font>';
                    }
                }
            ],
            tbar:Ext.create('Ext.toolbar.Toolbar',{
                items:['-',{
                    xtype:'label',
                    text:'人员名称:'
                },queryName,'-',{
                    xtype:'label',
                    text:'所属部门:'
                },queryDepartment,'-',{
                    xtype:'button',
                    iconCls:'find',
                    text:'查询',
                    handler:function(){
                        store.loadPage(1);
                    }
                },'-','->','-',{
                    xtype:'button',
                    iconCls:'create',
                    id:'createButton',
                    text:'新建',
                    handler:function(b,e){
                        key = 'create';
                        window.show(b);
                        form.getForm().reset();
                    }
                },'-',{
                    xtype:'button',
                    iconCls:'update',
                    text:'修改',
                    id:'updateButton',
                    handler:function(b,e){
                        key = 'update';
                        var selected = grid.getView().getSelectionModel().getSelection();
                        if(selected.length == 0){
                            Ext.Msg.show({title:'提示',msg:'请选择要修改记录',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                            return;
                        }
                        window.show(b);
                        var record = selected[0];
                        id = record.get('id');
                        form.getForm().loadRecord(record);
                    }
                },'-',{
                    xtype:'button',
                    iconCls:'delete',
                    text:'冻结',
                    handler:function(){
                        var selected = grid.getView().getSelectionModel().getSelection();
                        if(selected.length == 0){
                            Ext.Msg.show({title:'提示',msg:'请选择要冻结记录',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                            return;
                        }
                        var record = selected[0];
                        if(record.get('status') == 0){
                            Ext.Msg.show({title:'提示',msg:'记录为冻结状态',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                            return;
                        }
                        Ext.Msg.confirm('提示','确定冻结此用户?',function(btn){
                            if(btn == 'yes'){
                                var params = {};
                                params.id = record.get('id');
                                var post = {};
                                post.params = params;
                                post.url = '/fsxt/sys/sysOperator!deleteSysOperator.action';
                                ajaxSubmit.call(post);
                            }
                        })
                    }
                },'-',{
                    xtype:'button',
                    iconCls:'accept',
                    text:'启用',
                    handler:function(){
                        var selected = grid.getView().getSelectionModel().getSelection();
                        if(selected.length == 0){
                            Ext.Msg.show({title:'提示',msg:'请选择要启用记录',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                            return;
                        }
                        var record = selected[0];
                        if(record.get('status') == 1){
                            Ext.Msg.show({title:'提示',msg:'记录为启用状态',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                            return;
                        }
                        Ext.Msg.confirm('提示','确定启用此用户?',function(btn){
                            if(btn == 'yes'){
                                var params = {};
                                params.id = record.get('id');
                                var post = {};
                                post.params = params;
                                post.url = '/fsxt/sys/sysOperator!enableSysOperator.action';
                                ajaxSubmit.call(post);
                            }
                        })
                    }
                },'-']
            }),
            bbar: Ext.create('Ext.PagingToolbar', {
                store: store,
                displayInfo: true,
                displayMsg: '当前第 {0} - {1} 条记录,共 {2} 条',
                emptyMsg: "没有任何记录"
            }),
            renderTo:Ext.getBody()
        });

        Ext.apply(departmentCfg,{id:'departmentId',name:'departmentId',fieldLabel:'所属部门',labelAlign:'right',store:departmentStore});
        var department = Ext.create('Ext.form.ComboBox',departmentCfg);

        var form = Ext.create('Ext.form.Panel',{
            layout:{
                type: 'table',
                columns: 2
            },
            items:[{
                xtype:'textfield',
                fieldLabel:'人员名称',
                labelAlign:'right',
                id:'name',
                name:'name',
                allowBlank:false
            },department,{
                xtype:'combo',
                fieldLabel:'性别',
                labelAlign:'right',
                id:'sex',
                name:'sex',
                queryMode: 'local',
                displayField: 'label',
                valueField: 'value',
                allowBlank:false,
                store:  Ext.create('Ext.data.Store', {
                    fields: ['value', 'label'],
                    data : [
                        {"value":"m", "label":"男"},
                        {"value":"f", "label":"女"}
                    ]
                })
            },{
                xtype:'textfield',
                fieldLabel:'电话',
                labelAlign:'right',
                id:'phone',
                name:'phone'
            },{
                xtype:'textfield',
                fieldLabel:'邮箱',
                labelAlign:'right',
                id:'mail',
                name:'mail'
            },{
                xtype:'textfield',
                fieldLabel:'qq号码',
                labelAlign:'right',
                id:'qq',
                name:'qq'
            },{
                xtype:'textfield',
                fieldLabel:'身份证号',
                labelAlign:'right',
                id:'idCard',
                name:'idCard'
            }]
        });

        var window = Ext.create('Ext.window.Window', {
            title: '人员信息',
            layout:'fit',
            closeAction:'hide',
            resizable:false,
            autoHeight:true,
            width:600,
            plain: true,
            modal:true,
            draggable:true,
            items:form,
            buttons:[{
                text:'保存',
                handler:function(){
                    if(!form.getForm().isValid()){
                        Ext.Msg.show({title:'提示',msg:'验证失败,请检查数据',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                        return;
                    }
                    var data = form.getForm().getValues();
                    if(key == 'update'){
                        data.id = id;
                    }
                    var params = {};
                    params.data = encodeURIComponent(Ext.JSON.encode(data));
                    params.key = key;
                    var post = {};
                    post.url = '/fsxt/sys/sysOperator!saveSysOperator.action';
                    post.params = params;
                    ajaxSubmit.call(post);
                    window.hide();
                }
            },{
                text:'取消',
                handler:function(){
                    window.hide(Ext.get('createButton'));
                }
            }]
        });

        function ajaxSubmit(){
            Ext.MessageBox.wait("", "正在保存设置,请稍候...");
            Ext.Ajax.request({
                url:this.url,
                method: 'post',
                params: this.params,
                success:function(response,options){
                    Ext.MessageBox.hide();
                    var result = Ext.JSON.decode(response.responseText);
                    if(result.success){
                        Ext.Msg.show({title:'提示',msg:'恭喜!保存数据成功!',icon:Ext.MessageBox.INFO,buttons:Ext.MessageBox.OK,
                            fn:function(){store.loadPage(1);}
                        });
                    }else{
                        Ext.Msg.show({title:'提示',msg:result.msg,icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                    }
                },
                failure:function(response,options){
                    Ext.MessageBox.hide();
                    Ext.Msg.show({title:'提示',msg:'糟糕!保存设置失败.',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.ERROR});
                }
            });
        }

    });
})();
</script>
</head>
<body>
</body>
</html>